package com.gjy.mywork.controller;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gjy.mywork.config.MyWebSocket;
import com.gjy.mywork.util.AjaxReturnVO;

/**
 * websocket消息推送
 * 
 * @author gxy 2018年11月16日 my-work success when you begin
 */
@Controller
@RequestMapping(value = "/pm")
public class PushMessageController {

	@GetMapping(value = "/personalpage/{person}")
	public ModelAndView gotoPMPage(@PathVariable String person) {

		ModelAndView mav = new ModelAndView("/wbs/person");
		mav.addObject("cid", person);

		return mav;
	}

	// 推送数据接口
	@ResponseBody
	@RequestMapping("/push/{cid}")
	public AjaxReturnVO pushToWeb(@PathVariable String cid, String message) {

		try {
			MyWebSocket.sendInfo(message, cid);
		} catch (IOException e) {
			e.printStackTrace();
			return AjaxReturnVO.fail(e.getMessage());
		}
		return AjaxReturnVO.ok(cid);
	}

	// 推送数据接口
	@ResponseBody
	@RequestMapping("/push")
	public AjaxReturnVO pushToWeb(String message) {

		try {
			MyWebSocket.sendInfo(message, null);
		} catch (IOException e) {
			e.printStackTrace();
			return AjaxReturnVO.fail(e.getMessage());
		}
		return AjaxReturnVO.ok("all user");
	}

}
